using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Collections;

/// <summary>
/// Summary description for CheckInput
/// </summary>
/// 
namespace VMG.Format
{
    public class CheckInput
    {
        public CheckInput()
        {

        }

        public static bool CheckDateTime(string InPut)
        {
            try
            {
                string date = InPut.Trim();
                string[] dateArray = date.Split("/".ToCharArray());
                DateTime datenewB = new DateTime(Convert.ToInt32(dateArray[2]), Convert.ToInt32(dateArray[1]), Convert.ToInt32(dateArray[0]), 0, 0, 0);
                return true;
            }
            catch (System.FormatException)
            {
                return false;
            }
            catch (System.ArgumentOutOfRangeException)
            {
                return false;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static DateTime ConvertDateTime(string InPut)
        {
            string date = InPut.Trim();
            string[] dateArray = date.Split("/".ToCharArray());
            return new DateTime(Convert.ToInt32(dateArray[2]), Convert.ToInt32(dateArray[1]), Convert.ToInt32(dateArray[0]), 0, 0, 0);
        }

        public static bool CheckPhone(string strPhone)
        {
            bool result;
            string Phone = strPhone.Trim();
            if (Phone.Length <= 11 && Phone.Length >= 10)
            {
                if (Phone.Length == 10)
                {
                    if (Phone.Substring(0, 2) == "09")
                    {
                        result = CheckIntFormat(Phone);
                    }
                    else
                    {
                        result = false;
                    }
                }
                else
                {
                    if (Phone.Substring(0, 2) == "01")
                    {
                        result = CheckIntFormat(Phone);
                    }
                    else
                    {
                        result = false;
                    }
                }
            }
            else
            {
                result = false;
            }
            return result;
        }

        public static bool CheckIntFormat(string Phone)
        {
            try
            {
                if (Convert.ToInt32(Phone) >= 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (System.FormatException)
            {
                return false;
            }
            catch (System.OverflowException)
            {
                return false;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public static Hashtable SaveSchInfo(int groupID, int schType, string textSearch)
        {
            Hashtable textToSave = new Hashtable();
            textToSave.Add("groupID", groupID);
            textToSave.Add("schType", schType);
            textToSave.Add("textSearch", textSearch);
            return textToSave;
        }

        public static Hashtable SaveSchInfoNew(string groupID, int schType, string textSearch)
        {
            Hashtable textToSave = new Hashtable();
            textToSave.Add("groupID", groupID);
            textToSave.Add("schType", schType);
            textToSave.Add("textSearch", textSearch);
            return textToSave;
        }

        public static string cutstr(string s, string s0)
        {
            s = Codau2Khongdau(s + ",");
            string str;
            string kq = "";
            int len1 = s.IndexOf(s0, 0);
            while (len1 == 0)
            {
                s = s.Substring(1);
                len1 = s.IndexOf(s0, 0);
            }
            while (len1 > 0)
            {
                str = s.Substring(0, len1);
                if (str.Length == 11 || str.Length == 10)
                {
                    try
                    {
                        Convert.ToInt32(str);
                        kq += str + ",";
                    }
                    catch
                    {
                    }
                }
                s = s.Replace(str + ",", "");
                len1 = s.IndexOf(s0, 0);
                while (len1 == 0)
                {
                    s = s.Substring(1);
                    len1 = s.IndexOf(s0, 0);
                }
            }
            return kq;
        }

        public static string Codau2Khongdau(string str)
        {
            string codauChars = @"`~!@#$%^&*():;'.,?/|\- ";
            string kodauChars = @",,,,,,,,,,,,,,,,,,,,,,,";
            string retVal = String.Empty;
            if (str == null)
                return retVal;
            int pos;
            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] != ' ')
                {
                    pos = codauChars.IndexOf(str[i].ToString());
                    if (pos >= 0)
                        retVal += kodauChars[pos];
                    else
                        retVal += str[i];
                }
            }
            return retVal;
        }

        public static string cutstr(string p)
        {
            throw new NotImplementedException();
        }
    }

    public class Report
    {
        #region Field
        private DateTime datStartDate = DateTime.Now;
        private DateTime datEndDate = DateTime.Now;
        private int intSmsType = 0;
        private int intReType = 0;
        private string strPhone = String.Empty;
        #endregion

        #region Constructor
        public Report()
        {
        }
        public Report(DateTime StartDate, DateTime EndDate, int SmsType, int ReType, string Phone)
        {
            datStartDate = StartDate;
            datEndDate = EndDate;
            intSmsType = SmsType;
            intReType = ReType;
            strPhone = Phone;
        }
        #endregion

        #region Properties
        public DateTime StartDate
        {
            get { return datStartDate; }
            set { datStartDate = value; }
        }
        public DateTime EndDate
        {
            get { return datEndDate; }
            set { datEndDate = value; }
        }
        public int SmsType
        {
            get { return intSmsType; }
            set { intSmsType = value; }
        }
        public int ReType
        {
            get { return intReType; }
            set { intReType = value; }
        }
        public string Phone
        {
            get { return strPhone; }
            set { strPhone = value; }
        }
        #endregion
    }
    public class PageRecord : IComparable
    {
        #region Field
        private int pageNumber = 0;
        private int timeId = 0;
        #endregion

        #region Properties
        public int PageNumber
        {
            get { return pageNumber; }
            set { pageNumber = value; }
        }
        public int TimeId
        {
            get { return timeId; }
            set { timeId = value; }
        }
        #endregion

        #region Constructor
        public PageRecord()
        {
            pageNumber = 0;
            timeId = 0;
        }
        public PageRecord(int PageNumber, int TimeId)
        {
            pageNumber = PageNumber;
            timeId = TimeId;
        }
        #endregion

        public static PageRecord GetRecord(List<PageRecord> Recode, int TimeId)
        {
            PageRecord newRecode = new PageRecord();
            PageRecord tempRecode;
            bool exist = false;
            int i = 0;
            int count = Recode.Count;
            if (Recode.Count != 0)
            {
                while (!exist)
                {
                    tempRecode = (PageRecord)Recode[i];
                    if (tempRecode.TimeId == TimeId)
                    {
                        newRecode.TimeId = tempRecode.TimeId;
                        newRecode.PageNumber = tempRecode.PageNumber;
                        exist = true;
                    }
                    i++;
                    if (i == count)
                    {
                        exist = true;
                    }
                }
            }
            return newRecode;
        }

        public static bool RecordExist(List<PageRecord> Recode, int TimeId)
        {
            PageRecord tempRecode;
            bool exist = false;
            bool resuilt = false;
            int i = 0;
            int count = Recode.Count;
            if (Recode.Count != 0)
            {
                while (!exist)
                {
                    tempRecode = (PageRecord)Recode[i];
                    if (tempRecode.TimeId == TimeId)
                    {
                        resuilt = true;
                        exist = true;
                    }
                    i++;
                    if (i == count)
                    {
                        exist = true;
                    }
                }
            }
            return resuilt;
        }

        public static List<PageRecord> GetPageRecode(List<PageRecord> Recode, int PageNumber)
        {
            List<PageRecord> listRecode = new List<PageRecord>();
            PageRecord tempRecode;
            int count = Recode.Count;
            for (int i = 0; i < count; i++)
            {
                tempRecode = (PageRecord)Recode[i];
                if (tempRecode.PageNumber == PageNumber)
                {
                    PageRecord recodeObj = new PageRecord(tempRecode.PageNumber, tempRecode.TimeId);
                    listRecode.Add(recodeObj);
                }
            }
            return listRecode;
        }

        public static PageRecord GetRecordRef(List<PageRecord> Recode, int TimeId)
        {
            PageRecord newRecode = new PageRecord();
            PageRecord tempRecode;
            bool exist = false;
            int i = 0;
            int count = Recode.Count;
            if (Recode.Count != 0)
            {
                while (!exist)
                {
                    tempRecode = (PageRecord)Recode[i];
                    if (tempRecode.TimeId == TimeId)
                    {
                        exist = true;
                    }
                    i++;
                    if (i == count)
                    {
                        exist = true;
                    }
                }
            }
            return (PageRecord)Recode[i - 1];
        }

        public static bool CheckPageExist(List<PageRecord> Recode, int PageNumber)
        {
            PageRecord tempRecode;
            int i = 0;
            bool exist = false;
            bool resuilt = false;
            int count = Recode.Count;
            if (Recode.Count != 0)
            {
                while (!exist)
                {
                    tempRecode = (PageRecord)Recode[i];
                    if (tempRecode.PageNumber == PageNumber)
                    {
                        exist = true;
                        resuilt = true;
                    }
                    i++;
                    if (i == count)
                    {
                        exist = true;
                    }
                }
            }
            return resuilt;
        }

        public int CompareTo(object obj)
        {
            PageRecord fromit = (PageRecord)obj;
            DateTime fromDate = new DateTime(fromit.TimeId, fromit.PageNumber, 1);
            DateTime toDate = new DateTime(this.TimeId, this.PageNumber, 1);
            if (fromDate.CompareTo(toDate) == -1) return 1;
            if (fromDate.CompareTo(toDate) == 1) return -1;
            return 0;
        }
    }

    public class myReverserClass : IComparer
    {
        // Calls CaseInsensitiveComparer.Compare with the parameters reversed.
        int IComparer.Compare(Object from, Object to)
        {
            PageRecord fromit = (PageRecord)from;
            PageRecord toit = (PageRecord)to;
            DateTime fromDate = new DateTime(fromit.TimeId, fromit.PageNumber, 1);
            DateTime toDate = new DateTime(toit.TimeId, toit.PageNumber, 1);
            if (fromDate.CompareTo(toDate) == -1) return 1;
            if (fromDate.CompareTo(toDate) == 1) return -1;
            return 0;
        }
    }
}
